package com.intelligentmeterreadingsys.report.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.EngineeringOrderPoVo;
import com.intelligentmeterreadingsys.povo.ListViewInfo;
import com.intelligentmeterreadingsys.report.db.ReportDao;
import com.intelligentmeterreadingsys.waterlist.querywaterlist.activity.ListViewAdapter;

public class OrderListActivity extends Activity implements OnScrollListener {

    List<ListViewInfo> items = new ArrayList<ListViewInfo>();
    private ListView listView;
    private int visibleLastIndex = 0; // 最后的可视项索引
    private int visibleItemCount; // 当前窗口可见项总数
    private ListViewAdapter adapter;
    private View loadMoreView;
    private Button loadMoreButton;
    private Handler handler = new Handler();
    private int count = 0;// 总页数
    public OrderQueryMainActivity act = null;// 父类
    public List<EngineeringOrderPoVo> list = null;
    private int page = 1;// 当前页数

    public int getPage() {
	return page;
    }

    public void setPage(int page) {
	this.page = page;
    }

    public List<EngineeringOrderPoVo> getList() {
	return list;
    }

    public void setList(List<EngineeringOrderPoVo> list) {
	this.list = list;
    }

    public String getOrderid() {
	return orderid;
    }

    public ListView getListView() {
	return listView;
    }

    public void setListView(ListView listView) {
	this.listView = listView;
    }

    public void setOrderid(String orderid) {
	this.orderid = orderid;
    }

    private String orderid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.listview);
	loadMoreView = getLayoutInflater().inflate(R.layout.order_button, null);
	loadMoreButton = (Button) loadMoreView.findViewById(R.id.order_query_main_button);
	listView = (ListView) this.findViewById(R.id.listView1);

	listView.addFooterView(loadMoreView); // 设置列表底部视图
	// listView.addHeaderView(v) //设置列表顶部视图
	adapter = new ListViewAdapter(items, this);
	listView.setAdapter(adapter); // 自动为id是list的ListView设置适配器

	listView.setOnScrollListener(this); // 添加滑动监听

	listView.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		String orderid = list.get((page - 1) * 10 + arg2).getId() + "";

	    }
	});

	loadMoreButton.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		handler.postDelayed(new Runnable() {
		    @Override
		    public void run() {
			loadData();
			// visibleLastIndex - visibleItemCount + 1
			loadMoreButton.setText("确定"); // 恢复按钮文字

		    }
		}, 1000);
	    }
	});
	ReportDao dao = new ReportDao(this);
	list = dao.queryOrder(null);
	dao.close();
	loadData();
    }

    /**
     * 滑动时被调用
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
	this.visibleItemCount = visibleItemCount;
	visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
    }

    /**
     * 滑动状态改变时被调用
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
	int itemsLastIndex = adapter.getCount() - 1; // 数据集最后一项的索引
	int lastIndex = itemsLastIndex + 1; // 加上底部的loadMoreView项
	if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
	    // 如果是自动加载,可以在这里放置异步加载数据的代码
	    Log.i("LOADMORE", "loading...");
	}
    }

    /**
     * 模拟加载数据
     */
    private void loadData() {

	if (list != null && list.size() > 0) {
	    count = list.size() % 10 > 0 ? list.size() / 10 + 1 : list.size() / 10;
	}

	if (page >= count) {
	    Toast.makeText(this, "已到最后一页了", 1000).show();
	    page = count;

	}
	if (count == 0) {
	    adapter.removeItem();
	    adapter.notifyDataSetChanged(); // 数据集变化后,通知adapter
	    ListViewInfo info = new ListViewInfo();
	    info.setNumber("没有数据..........");
	    items.add(info);
	    listView.setSelection(0); // 设置选中项

	} else {
	    adapter.removeItem();
	    adapter.notifyDataSetChanged(); // 数据集变化后,通知adapter

	    listView.setSelection(visibleLastIndex - visibleItemCount + 1); // 设置选中项

	    if (count == 0) {
		Toast.makeText(this, "没有查到数据", 1000).show();
		ListViewInfo info = new ListViewInfo();
		info.setNumber("没有数据..........");
		items.add(info);
		return;
	    }

	    for (int i = (page - 1) * 10; i < (page * 10 > list.size() ? list.size() : page * 10); i++) {

		EngineeringOrderPoVo e = list.get(i);
		ListViewInfo info = new ListViewInfo();
		info.setNumber(i + "");
		info.setUserinfo(e.getName() + "  " + e.getDesc() + "(" + e.getTypename() + ")");
		items.add(info);

	    }
	}

    }

}
